package com.person.jc.web.service;

import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.person.jc.common.Constants;
import com.person.jc.entity.TsUser;
import com.person.jc.service.finance.BsOrderBillingService;
import com.person.jc.service.service.BsBillingService;
import com.person.jc.utils.JsonMapper;
import com.person.jc.utils.Servlets;
import com.person.jc.utils.SessionKit;
import com.person.jc.utils.Utils;

public class BsBillingController extends Controller{
	public void init() {
		setAttr("menuId", getPara("menuId"));
		render("main.jsp");
	}
	public void list(){
		Map<String, Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		List<Record> list = BsBillingService.me.getList(map);
//		if(list!=null&&list.size()>0) {
//			for(Record record:list) {
//				List<Record> detailList = BsBillingService.me.getDetailList((String)record.get("order_id"));
//				record.set("certificate", detailList);
//			}
//		}
		setAttr("list", list);
		render("list.jsp");
	}
	
	
	/**
	 * 未处理的开票申请和未完全回款的单据(财务的)
	 */
	public void getBillingCount(){
		List<Record> list = BsOrderBillingService.me.getUntreatedCount();
		setAttr("untreatedCount", list!=null?list.size():0);
		
		List<Record> list1 = BsOrderBillingService.me.getIncompleteCount();
		setAttr("incompleteCount", list1!=null?list1.size():0);
		render("indexCount.jsp");
	}
	
	/**
	 * 未处理的开票申请和未完全回款的单据(销售的)
	 */
	public void getBillingSalelingCount(){
		TsUser tsUser = (TsUser)SessionKit.get().getAttribute(Constants.LOGIN_USER);
		List<Record> list = BsOrderBillingService.me.getUntreatedSCount(tsUser.getUserId());
		setAttr("untreatedSCount", list!=null?list.size():0);
		render("indexBCount.jsp");
	}
	
	
	public void edit(){
		String orderId = getPara("orderId");
		String status = getPara("status");
		if(Utils.isNotEmpty(orderId)){
			Record order = null;
			List<Record> detail = null;
			if(status.equals("0")){   // 申请
				order = BsBillingService.me.getOrderInfo(orderId);
				detail = BsBillingService.me.getOrderDetail(orderId);
			}else{                    // 编辑
				order = BsBillingService.me.getBillingInfo(orderId);
				detail = BsBillingService.me.getBillingDetail(orderId);
			}
			order.set("status", status);
			setAttr("obj", order);
			setAttr("details", detail);
			if(detail.size() > 0){
				setAttr("detailsJson", JsonMapper.nonDefaultMapper().toJson(detail));
			}else{
				setAttr("detailsJson", "[]");
			}
		}else{
			setAttr("detailsJson", "[]");
		}
	}
	
	@Before(Tx.class)
	public void save(){
		Map<String, Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		String status = String.valueOf(map.get("status"));
		if(status.equals("0")) {
			TsUser tsUser = (TsUser)SessionKit.get().getAttribute(Constants.LOGIN_USER);
			map.put("optUser", tsUser.getUserId());
			BsBillingService.me.save(map);
		}else {
			BsBillingService.me.update(map);
		}
		renderText("success");
	}
}
